<?php

namespace app\home\controller\team;

use app\BaseController;
use app\common\model\MoneyLog;
use app\common\model\User;
use app\common\model\UserRealName;
use app\common\model\TeamTongji;
use app\common\model\TeamPaiMing;
use app\common\model\TeamJiangLi;
use app\common\traites\PublicCrudTrait;

use hg\apidoc\annotation as Apidoc;

/**
 *
 * @Apidoc\Title("团队管理")
 * */
class Team extends BaseController
{
    use PublicCrudTrait;

    /**
     * @var TouziLottery
     */
    protected $model;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * @Apidoc\Title("我的团队")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function team() {
        $uid = session('home_user.id');
        $res = TeamTongji::where('user_id', $uid)->field('team_has_fund_1,team_has_nums_1')->find();
        return show($res);
    }

    /**
     * @Apidoc\Title("团队奖励")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function team_jiangli() {
        $res = TeamJiangLi::where([])->select();
        $lingquCheck = $this->team_jiangli_check();
        foreach($res as $key => $val){
            $res[$key]['is_ky_lingqu'] = $lingquCheck[$val['team_nums']];
        }
        return show($res);
    }


    /**
     * @Apidoc\Title("团队奖励")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function team_paiming() {
        $post = $this->request->post();
        $pai_type = $post['pai_type'];
        $res = TeamPaiMing::where(['pai_type'=>$pai_type])->order('id asc')->select();
        return show($res);
    }

    // 
    /**
     * @Apidoc\Title("团队奖励")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function team_jiangli_lingqu() {
        $post = $this->request->post();
        $tuandui_lingqu_id = $post['id'];
        $uid = session('home_user.id');

        $userInfo = (new User())->where(['id'=>$uid])->find();
        $lingqu = is_null($userInfo['tuandui_lingqu'])?[]:unserialize($userInfo['tuandui_lingqu']);

        // 如果没有
        if(count($lingqu) ==0){
            $lingqu['1'] = 0;
            $lingqu['3'] = 0;
            $lingqu['5'] = 0;
            $lingqu['10'] = 0;
            $lingqu['20'] = 0;
            $lingqu['30'] = 0;

            $data_update = [];
            $data_update['tuandui_lingqu'] = serialize($lingqu);
            (new User())->where(['id'=>$uid])->update($data_update);
        }

        if($lingqu[$tuandui_lingqu_id] == 1){
            return show([],config('ToConfig.http_code.error'),'领取过了');
        }
        // 统计一级团队人数
        $map = [];
        $map['agent_id_1'] = $uid;
        $nums = (new User())->where($map)->whereTime('create_time', 'today')->count('*');

        // 领取奖励
        $money_juanzeng = 0;
        if($lingqu[$tuandui_lingqu_id] == 0 && $tuandui_lingqu_id == 1 && $nums >= $tuandui_lingqu_id){
            $lingqu[$tuandui_lingqu_id] = 1;
            $money_juanzeng = 50000;
        }
        if($lingqu[$tuandui_lingqu_id] == 0 && $tuandui_lingqu_id == 3 && $nums >= $tuandui_lingqu_id){
            $lingqu[$tuandui_lingqu_id] = 1;
            $money_juanzeng = 100000;
        }
        if($lingqu[$tuandui_lingqu_id] == 0 && $tuandui_lingqu_id == 5  && $nums >= $tuandui_lingqu_id){
            $lingqu[$tuandui_lingqu_id] = 1;
            $money_juanzeng = 200000;
        }
        if($lingqu[$tuandui_lingqu_id] == 0 && $tuandui_lingqu_id == 10  && $nums >= $tuandui_lingqu_id){
            $lingqu[$tuandui_lingqu_id] = 1;
            $money_juanzeng = 400000;
        }
        if($lingqu[$tuandui_lingqu_id] == 0 && $tuandui_lingqu_id == 20  && $nums >= $tuandui_lingqu_id){
            $lingqu[$tuandui_lingqu_id] = 1;
            $money_juanzeng = 800000;
        }
        if($lingqu[$tuandui_lingqu_id] == 0 && $tuandui_lingqu_id == 30  && $nums >= $tuandui_lingqu_id){
            $lingqu[$tuandui_lingqu_id] = 1;
            $money_juanzeng = 1500000;
        }

        $data_update = [];
        $data_update['tuandui_lingqu'] = serialize($lingqu);
        (new User())->where(['id'=>$uid])->inc('money_juanzeng',$money_juanzeng)->update($data_update);
        MoneyLog::flowing(1, 1, 1, 103, session('home_user'), $money_juanzeng, 90997, '团队捐赠', 'money_juanzeng');

        return show([]);
    }


        /**
     * @Apidoc\Title("团队奖励检查")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function team_jiangli_check() {
        $uid = session('home_user.id');
        $userInfo = (new User())->where(['id'=>$uid])->find();
        $lingqu = is_null($userInfo['tuandui_lingqu'])?[]:unserialize($userInfo['tuandui_lingqu']);

        // 如果没有
        if(count($lingqu) ==0){
            $lingqu['1'] = 0;
            $lingqu['3'] = 0;
            $lingqu['5'] = 0;
            $lingqu['10'] = 0;
            $lingqu['20'] = 0;
            $lingqu['30'] = 0;
        }

        // 统计一级团队人数
        $map = [];
        $map['agent_id_1'] = $uid;
        $nums = (new User())->where($map)->count('*');

        // 人数不足
        if($nums < 1 && $lingqu['1'] == 0){
            $lingqu['1'] = 2;
        }
        // 人数不足
        if($nums < 3 && $lingqu['3'] == 0){
            $lingqu['3'] = 2;
        }
        // 人数不足
        if($nums < 5 && $lingqu['5'] == 0){
            $lingqu['5'] = 2;
        }
        // 人数不足
        if($nums < 10 && $lingqu['10'] == 0){
            $lingqu['10'] = 2;
        }
        // 人数不足
        if($nums < 20 && $lingqu['20'] == 0){
            $lingqu['20'] = 2;
        }
        // 人数不足
        if($nums < 30 && $lingqu['30'] == 0){
            $lingqu['30'] = 2;
        }

        return $lingqu;
    }
    /**
     * @Apidoc\Title("各级团队成员")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("type", type="string",require=true, desc="等级1，2，3")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function team_level_member() {
        $uid = session('home_user.id');
        //过滤数据
        $postField = 'type';
        $params = $this->request->only(explode(',', $postField), 'post', null);
        if (!isset($params['type'])) {
            $params['type'] = 1;
        }
        if ($params['type'] == 1) {
            $user = (new User())->field('*')->limit(20)->where('agent_id_1', $uid)->order('id','desc')->select()->toArray();
        } elseif ($params['type'] == 2) {
            $user = (new User())->field('*')->limit(20)->where('agent_id_2', $uid)->order('id','desc')->select()->toArray();
        } else {
            $user = (new User())->field('*')->limit(20)->where('agent_id_3', $uid)->order('id','desc')->select()->toArray();
        }
        if ($user) {
            foreach ($user as $k => $v) {
                $user[$k]['phone'] = ysPhone($v['phone']);

                $userRealName = (new UserRealName())->where('user_id', $v['id'])->find();
                if ($v['is_real_name'] == 0) {
                    $user[$k]['true_name'] = '未实名';
                }
                if (!empty($userRealName)) {
                    $user[$k]['true_name'] = $userRealName->true_name;
                }
            }
        }
        return show($user);
    }



    /**
     * @Apidoc\Title("各级团队人数")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("type", type="string",require=true, desc="等级1，2，3")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function team_level_nums() {
        $nums = [
            'level_1'=>0,
            'level_2'=>0,
            'level_3'=>0,
        ];
        $uid = session('home_user.id');
        $nums['level_1'] = (new User())->where(['agent_id_1'=>$uid])->count('*');
        $nums['level_2'] = (new User())->where(['agent_id_2'=>$uid])->count('*');
        $nums['level_3'] = (new User())->where(['agent_id_3'=>$uid])->count('*');

        return show($nums);
    }








// 类结束了     
}
